<?php

class Kd_Html_Grid_Column extends Kd_Html_Element {

    private $_title = '';
    private $_name = '';
    private $_sortField = '';
    private $_head = NULL;
    private $_cell = NULL;
    private $_grid = NULL;
    private $_filter = NULL;
    private $_width = 0;
    private $_align = 'left';

    public function __construct() {
        parent::__construct();
    }

    public function getHead() {
        return $this->_head;
    }

    public function getCell() {
        return $this->_cell;
    }

    public function getAlign(){
    	return $this->_align;
    }
    public function setAlign($value){
    	$this->_align = $value;
    	return $this;
    }

    public function getWidth(){
    	return $this->_width;
    }
    public function setWidth($value){
    	$this->_width = $value;
    	return $this;
    }

    public function getFilter(){
        return $this->_filter;
    }
    public function setFilter($value){
        $this->_filter = $value;
        return $this;
    }

    public function removeFilter(){
        $this->_filter = NULL;
        return $this;
    }

    public function setTextboxFilter($id = NULL, $name = NULL, $text = ''){
        $id = is_null($id)? 'filter_'.$this->getName(): $id;
        $name = is_null($name)? 'filter['.$this->getName().']': $name;
        $this->_filter = new Kd_Html_Form_Textbox();
        $this->_filter->setId($id)
                ->setName($name)
                ->setValue($text);
        return $this;
    }

    public function setSelectFilter($id = NULL, $name = NULL, $data = array(), $labelKey = NULL, $valueKey = NULL, $value = NULL){
        $id = is_null($id)? 'filter_'.$this->getName(): $id;
        $name = is_null($name)? 'filter['.$this->getName().']': $name;
        $this->_filter = new Kd_Html_Form_Select();
        $this->_filter->setId($id)
                ->setName($name)
                ->setOption('', '')
                ->bindData($data, $labelKey, $valueKey)
                ->setValue($value);
        return $this;
    }

    public function setBooleanFilter($id = NULL, $name = NULL, $value = NULL){
        $id = is_null($id)? 'filter_'.$this->getName(): $id;
        $name = is_null($name)? 'filter['.$this->getName().']': $name;
        $yesOption = Kd_Html_Config::getYesOption();
        $noOption = Kd_Html_Config::getNoOption();
        $this->_filter = new Kd_Html_Form_Select();
        $this->_filter->setId($id)
                ->setName($name)
                ->setOption('', '')
                ->setOption($yesOption['label'], $yesOption['value'])
                ->setOption($noOption['label'], $noOption['value'])
                ->setValue($value);
        return $this;
    }

    public function getGrid() {
        return $this->_grid;
    }
    public function setGrid($value) {
        $this->_grid = $value;
        return $this;
    }

    public function getTitle() {
        return $this->_title;
    }
    public function setTitle($value) {
        $this->_title = $value;
        return $this;
    }

    public function getName() {
        return $this->_name;
    }
    public function setName($value) {
        $this->_name = $value;
        return $this;
    }

    public function getSortField() {
        return $this->_sortField;
    }
    public function setSortField($value) {
        $this->_sortField = $value;
        return $this;
    }

    public function createHead($type = 'label') {
        $class = 'Kd_Html_Grid_Head_'.ucfirst($type);
        $this->_head = new $class();
        $this->_head->setColumn($this);
        return $this->_head;
    }

    public function createCell($type = 'label') {
        $class = 'Kd_Html_Grid_Cell_'.ucfirst($type);
        $this->_cell = new $class();
        $this->_cell->setColumn($this);
        return $this->_cell;
    }

    protected function render() {
        $this->addClass('col-'.$this->getName());
        return '<col '.$this->getAttributeHtml().'/>';
    }
}